package com.example.qxfw.rcyw.task;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.qxfw.common.entity.yj.WarnMonitorInfo;
import com.example.qxfw.rcyw.mapper.WarnMoniInfoMapper;
import com.example.qxfw.rcyw.service.WarnMoniInfoService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

/**
 * @author: xwx
 * @description: 预警预报定时任务
 * @date: 2024/5/8
 * @version: 1.0
 */
@Component
@Slf4j
public class YjybTask extends QuartzJobBean {
    //注入气象要素查询的接口
    @Autowired
    private WarnMoniInfoService warnMoniInfoService;
    @Autowired
    private WarnMoniInfoMapper warnMoniInfoMapper;

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("{}", jobExecutionContext);
        //调用气象要素查询接口
        //用三天前的数据作为预报的基准
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_WEEK, calendar.get(Calendar.DAY_OF_WEEK) - 3);
        String warningTime = sdf.format(calendar.getTime());
        Timestamp timestamp = Timestamp.valueOf(warningTime);
        List<WarnMonitorInfo> warnMoniInfos = warnMoniInfoMapper.selectWarnMoniInfo(timestamp, "制作系统");
        //监控数据中的监控值，当监控值大于30时，将isremoved设置为零
        int count = 0;//记录更新的记录数
        BigDecimal bigDecimal = new BigDecimal(30);
        for (WarnMonitorInfo warnMoniInfo : warnMoniInfos) {
            if (warnMoniInfo.getMonitorvalue().compareTo(bigDecimal) > 0){
                WarnMonitorInfo info = new WarnMonitorInfo();
                info.setIsrevised(true);
                LambdaUpdateWrapper<WarnMonitorInfo> updateWrapper = new LambdaUpdateWrapper<>();
                updateWrapper.eq(WarnMonitorInfo::getId, warnMoniInfo.getId());
                count += warnMoniInfoMapper.update(info, updateWrapper);
                log.info("更新了{}条预警预报数据", count);
            }
        }
    }
}
